package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.notice.api.SysNoticeService;
import com.nidu.demo.notice.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 通知公告管理
 *
 * @author auto
 */
@Tag(name = "管理后台 - 通知公告")
@RestController
@RequestMapping("/system/notice")
@Validated
@RequiredArgsConstructor
public class SysNoticeController {

    private final SysNoticeService noticeService;

    @Operation(summary = "创建通知公告", description = "创建新的通知公告")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:notice:create')")
    public SingleResponse<NoticeCO> addNotice(@RequestBody NoticeAddCmd cmd) {
        return noticeService.add(cmd);
    }

    @Operation(summary = "更新通知公告", description = "更新通知公告的信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:notice:update')")
    public SingleResponse<NoticeCO> updateNotice(@PathVariable Long id, @RequestBody NoticeUpdateCmd cmd) {
        cmd.setId(id);
        return noticeService.update(cmd);
    }

    @Operation(summary = "更新通知公告状态", description = "更新通知公告的启用/禁用状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("@ss.hasPermission('system:notice:update')")
    public Response updateNoticeStatus(@PathVariable Long id, @RequestParam StatusEnum status) {
        return noticeService.updateStatus(id, status);
    }

    @Operation(summary = "删除通知公告", description = "根据 ID 删除通知公告")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:notice:delete')")
    public Response deleteNotice(@PathVariable Long id) {
        return noticeService.delete(id);
    }

    @Operation(summary = "批量删除通知公告", description = "根据 ID 列表删除多个通知公告")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:notice:delete')")
    public Response deleteNotices(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return noticeService.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取通知公告详情", description = "根据 ID 获取通知公告详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:notice:query')")
    public SingleResponse<NoticeCO> getNoticeById(@PathVariable Long id) {
        return noticeService.getById(id);
    }

    @Operation(summary = "查询通知公告列表", description = "获取所有通知公告信息")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:notice:query')")
    public MultiResponse<NoticeCO> listNotices(NoticeListQry qry) {
        return noticeService.list(qry);
    }

    @Operation(summary = "分页查询通知公告", description = "分页获取通知公告信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:notice:query')")
    public PageResponse<NoticeCO> pageNotices(NoticePageQry qry) {
        return noticeService.page(qry);
    }

}
